package com.olap.starter.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.olap.starter.core.base.BaseController;
import com.olap.starter.core.response.Result;
import com.olap.starter.pojo.FormDesigner;
import com.olap.starter.service.FormDesignerServices;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author WBG
 * @since 2023-12-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/online/formDesigner")
public class FormDesignerController extends BaseController<FormDesigner, FormDesignerServices> {
    private final FormDesignerServices formDesignerServices;

    @PostMapping({"/saveForm"})
    public Result<?> save(@RequestBody FormDesigner formDesigner) {
        if (formDesigner.getDescCode().isEmpty()) {
            return error("表单编码不能为空！");
        }
        LambdaQueryWrapper<FormDesigner> wrapper = Wrappers.lambdaQuery(FormDesigner.class).eq(FormDesigner::getDeleted, 0);
        wrapper.eq(FormDesigner::getDescCode, formDesigner.getDescCode());
        long count = formDesignerServices.count(wrapper);
        if (count > 0) {
            return error("表单编码已存在！");
        }
        formDesignerServices.save(formDesigner);
        return success("保存成功！");
    }

    @GetMapping({"/findList"})
    public Result<?> find(@RequestParam(value = "key", defaultValue = "") String key,
                          @RequestParam(value = "limit", defaultValue = "30") Integer limit) {
        List<FormDesigner> list = formDesignerServices.findList(key, limit);
        return success(list, "操作成功！");
    }


}
